package com.jamlu.framework.ui.status;

import android.app.Dialog;
import android.content.DialogInterface;

import com.jamlu.framework.R;
import com.jamlu.framework.ui.view.IBaseView;
import com.jamlu.framework.utils.ToastPts;

import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.disposables.Disposable;

/**
 * 显示对话框式的请求，可以选择按返回取消
 * Created by DMing at 2018/2/23 0023
 */

class StatusDialog implements IStatus {

    private IBaseView iBaseView;
    private boolean cancelable;//可以按返回取消
    private CompositeDisposable mCompositeDisposable;
    private Disposable mDisposable;//取消
    private Dialog mProgressDialog;


    StatusDialog(IBaseView iBaseView, CompositeDisposable mCompositeDisposable, boolean cancelable, StatusRequest statusRequest) { //每次都是new出来的，上面静态包裹
        this.iBaseView = iBaseView;
        this.mCompositeDisposable = mCompositeDisposable;
        this.cancelable = cancelable;
        switchStatus(LOADING);
        mDisposable = statusRequest.run(StatusDialog.this);
        mCompositeDisposable.add(mDisposable);
    }

    /**
     *
     * @param status 状态
     * @param obj 可选传入参数，可能为空
     */
    @Override
    public void switchStatus(int status, Object... obj) {
        switch (status){
            case LOADING:
                showProgressDialog(cancelable);
                break;
            case SUCCESS:
                dismissProgressDialog();
                break;
            case NOTHING:
                dismissProgressDialog();
                ToastPts.show(iBaseView.getContext().getString(R.string.status_nothing));
                break;
            case ERROR_RESPOND:
                dismissProgressDialog();
                if (obj != null && obj.length == 1) {
                    ToastPts.show(String.format("%s%s", iBaseView.getContext().getString(R.string.retry), obj[0]));
                }
                break;
            case ERROR_NETWORK:
                dismissProgressDialog();
                ToastPts.show(iBaseView.getContext().getString(R.string.status_error));
                break;
            case NO_NETWORK:
                dismissProgressDialog();
                ToastPts.show(iBaseView.getContext().getString(R.string.status_no_net));
                break;
        }
    }

    /**
     * 使用完结的时候调用
     */
    @Override
    public void releaseRef() {
        if (mDisposable != null && mCompositeDisposable != null)mCompositeDisposable.remove(mDisposable);
        iBaseView = null;
        mCompositeDisposable = null;
        mDisposable = null;
    }

    /**
     * 显示加载对话框
     */
    private void showProgressDialog(boolean cancelable) {
        mProgressDialog = iBaseView.createNewProgressDialog();
        mProgressDialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
            @Override
            public void onDismiss(DialogInterface dialog) {
                releaseRef();  //dialog消失，生命完结
            }
        });
        iBaseView.showProgressDialog(mProgressDialog,cancelable);
    }

    /**
     * 隐藏加载对话框
     */
    private void dismissProgressDialog() {
        iBaseView.dismissProgressDialog(mProgressDialog);
    }


}
